define([
    "ngController"
],function (appAngular) {

    // 侧栏组件（内部）
    appAngular.directive("bimApiSlider", function ($http) {
        return {
            restrict: "EA",
            replace: true,
            template: `<div class="model-slider">
						<div class="layer-opacity-slider"></div>
						<div class="model-slider-content">
							<details 
							    ng-if="modelSliderAjax!==''&&modelSliderAjax!==null&&modelSliderAjax.length > 0" 
							    ng-repeat="slider in modelSliderAjax" open>
								<summary>
								    <label><input type="checkbox" name="levelOne" ng-model="slider.check" 
                                        ng-change="modelSliderCheckOneChange(this, $index)"><span></span></label>
                                    <span>{{slider.name}}</span>
                                </summary>
								<details ng-repeat="item in slider.data">
									<summary>
									    <label><input type="checkbox" name="levelTwo" ng-model="item.check"
									        ng-change="modelSliderCheckTwoChange(this, $index)"><span></span></label>
									    <span>{{item.name}}</span>
									</summary>
									<p ng-repeat="list in item.data">
										<label><input type="checkbox" name="levelThree" ng-model="list.check"><span></span></label>
										<span ng-click="ajaxModelProperty(slider.modelId , list.id); 
										    modelSliderToModel(list.id)">{{list.name}}</span>
									</p>
								</details>
							</details>
							<details ng-if="modelSliderAjax==''||modelSliderAjax==null">
								<summary><span>暂无数据</span></summary>
								<p>
									<img ng-src="{{baseUrl+imgIconModel}}" />
									<span>暂无数据</span>
								</p>
							</details>
						</div>
						<div class="model-slider-shrink">
					    	<img ng-if="!sliderShrink" ng-src="{{baseUrl + imgSliderPrevious}}" 
					    	ng-click="modelSliderShrink()">
					    	<img ng-if="sliderShrink" ng-src="{{baseUrl + imgSliderSpreadRight}}" 
					    	ng-click="modelSliderSpread()">
					    </div>
				    </div>`,
            link: function ($scope, $elem, attrs) {
                // 侧栏是否展开
                $scope.sliderShrink = false;
                // 侧栏属性-对应模型名称-Model.body.name
                $scope.modelBodyName = Model.body ? Model.body.name : "";
                // 模型结构-目录
                $scope.modelSliderAjax = [];
                // 侧栏计数（多模型）
                $scope.sliderTreeCurrentIndex = 0;
                // 侧栏目录多选（一级目录CheckBox改变）
                $scope.modelSliderCheckOneChange = function (event, index) {
                    console.log(event);
                    console.log(index);
                    /*for (let i = 0; i < $scope.modelSliderAjax.length; i++) {
                        $scope.modelSliderAjax[i].check = event.modelSliderCheck;
                        for (let j = 0; j < $scope.modelSliderAjaxLevel3[i].length; j++) {
                            $scope.modelSliderAjaxLevel3[i][j].check = event.modelSliderCheck;
                        }
                    }*/
                };
                // 侧栏目录多选（二级目录CheckBox改变）
                $scope.modelSliderCheckTwoChange = function (event, index) {
                    console.log(event);
                    console.log(index);
                    /*for (let i = 0; i < $scope.modelSliderAjaxLevel3[index].length; i++) {
                        $scope.modelSliderAjaxLevel3[index][i].check = id.item.check;
                    }*/
                };

                // watch
                // 侧栏选中状态切换
                $scope.$watch("modelSliderAjax", function (newValue, oldValue) {
                    if (newValue == "") {

                    } else {

                    }
                });
                // 模型对应属性（这个方法缺个参数）
                $scope.$watch("modelBodyName", function (newValue, oldValue) {
                    if (newValue !== oldValue) {
                        // $scope.ajaxModelProperty(newValue);
                        $scope.ajaxModelPropertySize(newValue);
                        $scope.ajaxModelPropertyCustom();
                    }
                }, true);
                // $http
                // 结构树
                // ../model/tree
                $scope.sliderTreeInit = function () {
                    if ($scope.sliderTreeCurrentIndex >= $scope.modelId.length) {
                        return;
                    }
                    $http({
                        method: 'post',
                        url: $scope.ajaxUrl + '/model/tree',
                        headers: {
                            'Content-Type': "application/x-www-form-urlencoded; charset=UTF-8"
                        },
                        data: $.param({
                            UserId: $scope.userId,
                            ModelId: $scope.modelId[$scope.sliderTreeCurrentIndex]
                        }),
                    }).then(function successCallback(response) {
                        console.warn(response.data);
                        // 结构树没有内容时
                        if (response.data == "" || response.data == null || response.data.code != 1) {
                            $scope.sliderTreeCurrentIndex++;
                            $scope.sliderTreeInit();
                            return;
                        }
                        let param = {
                            userId: $scope.userId,
                            modelId: $scope.modelId[$scope.sliderTreeCurrentIndex],
                            check: false,
                            name: "",
                            data: []
                        };
                        for (let key in response.data.data[0]){
                            // console.warn(key);
                            // console.warn(response.data.data[0][key]);
                            if (key == "name") {
                                param.name = response.data.data[0][key];
                            } else {
                                let data = {
                                    check: false,
                                    name: key,
                                    data: []
                                };
                                for (let keyLevel3 in response.data.data[0][key]){
                                    // console.log(keyLevel3);
                                    // console.warn(response.data[key][keyLevel3]);
                                    let dataList = {
                                        check: false,
                                        id: response.data.data[0][key][keyLevel3].id,
                                        name: response.data.data[0][key][keyLevel3].name
                                    };
                                    data.data.push(dataList);
                                }
                                param.data.push(data);
                            }
                        }
                        $scope.modelSliderAjax.push(param);
                        $scope.sliderTreeCurrentIndex++;
                        $scope.sliderTreeInit();
                        // console.log(param);
                        console.log($scope.modelSliderAjax);
                    }, function errorCallback(response) {
                        // 请求失败执行代码
                        console.error("初始化：侧栏目录：ajax请求失败");
                    });
                };
                // 基本属性
                // ../model/property
                $scope.ajaxModelProperty = function (modelId, qid) {
                    console.log(qid);
                    $scope.ajaxModelPropertySize(qid);
                    $scope.ajaxModelPropertyCustom();
                    $http({
                        method: 'post',
                        url: $scope.ajaxUrl + '/model/property',
                        headers: {
                            'Content-Type': "application/x-www-form-urlencoded; charset=UTF-8"
                        },
                        data: $.param({
                            UserId: $scope.userId,
                            ModelId: modelId,
                            MemberId: qid
                        }),
                    }).then(function successCallback(response) {
                        console.warn(response.data);
                        /*if (response.data.code != 1) {
                            return;
                        }*/
                        $scope.modelProperty.property[0].data[1].data[0].ajaxTitle = true;
                        if (response.data.code != 1) {
                            $scope.modelProperty.property[0].data[1].data[0].title = response.data.data;
                        } else {
                            $scope.modelProperty.property[0].data[1].data[0].title = {
                                id: response.data.id,
                                property: []
                            };
                            for (let key in response.data.data[0]) {
                                for (let keyLevel in response.data.data[0][key]){
                                    let data = {
                                        name: keyLevel,
                                        value: response.data.data[0][key][keyLevel]
                                    };
                                    $scope.modelProperty.property[0].data[1].data[0].title.property.push(data);
                                }
                            }
                        }
                    }, function errorCallback(response) {
                        // 请求失败执行代码
                        console.error("模型属性获取失败！");
                    });
                };
                // 属性中-尺寸信息；
                // API.scaleplate('0h30grmMv32QH2pXqq7fmO')
                $scope.ajaxModelPropertySize = function(guid) {
                    $scope.modelProperty.property[0].data[0].data[0].title = {
                        id: '',
                        property: []
                    };
                    if (Model.scaleplate(guid) == undefined){
                        $scope.modelProperty.property[0].data[0].data[0].ajaxTitle = false;
                    } else {
                        $scope.modelProperty.property[0].data[0].data[0].ajaxTitle = true;
                        for (let key in Model.scaleplate(guid)) {
                            let data = {
                                name: key,
                                value: (Model.scaleplate(guid)[key] == undefined)?'暂无数据':Model.scaleplate(guid)[key]
                            };
                            $scope.modelProperty.property[0].data[0].data[0].title.property.push(data);
                        }
                    }
                };
                // 属性中-自定义属性
                // Model.modelDatas()
                $scope.ajaxModelPropertyCustom = function() {
                    $scope.modelProperty.property[0].data[2].data[0].title = {
                        id: '',
                        property: []
                    };
                    if (Model.modelDatas() == undefined){
                        $scope.modelProperty.property[0].data[2].data[0].ajaxTitle = false;
                    } else {
                        $scope.modelProperty.property[0].data[2].data[0].ajaxTitle = true;
                        for (let key in Model.modelDatas()) {
                            let data = {
                                name: key,
                                value: Model.modelDatas()[key]
                            };
                            $scope.modelProperty.property[0].data[2].data[0].title.property.push(data);
                        }
                    }
                };
                // 初始化-数据；
                $scope.sliderTreeInit();
            }
        };
    });

    return appAngular;
});